package com.woniuxy.education.service.impl;


import com.woniuxy.education.entity.*;
import com.woniuxy.education.mapper.HomeworkAssignmentsMapper;
import com.woniuxy.education.mapper.MarkTestsMapper;
import com.woniuxy.education.service.HomeworkAssignmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class HomeworkAssignmentsServiceImpl implements HomeworkAssignmentsService {
    @Autowired
    HomeworkAssignmentsMapper homeworkAssignmentsMapper;
    @Autowired
    MarkTestsMapper markTestsMapper;

  //  @Transactional
    @Override
    public void insertHomework(HomeworkAssignments homeworkAssignments,
                               HomeworkTypeNumber homeworkTypeNumber) {




        //添加作业信息
        homeworkAssignmentsMapper.insertHomework(homeworkAssignments);


       //创建存储题目对象
        HomeworkTitle homeworkTitle=new HomeworkTitle();
        //h_id赋值
        homeworkTitle.setH_id(homeworkAssignments.getId());
        //查询题目，给科目赋值
        homeworkTypeNumber.setSubject(homeworkAssignments.getSubject());
        if(homeworkTypeNumber.getChooseNumber()>0){
            //布置指定数量的选择题

            List<Integer> chooses= homeworkAssignmentsMapper
                    .pageFindChoose(homeworkTypeNumber);

            StringBuffer str=new StringBuffer();
            for (int i=0;i<chooses.size();i++) {
             if(i==chooses.size()-1) {
                 str.append(chooses.get(i));
             }else  {
                 str.append(chooses.get(i)+",");
             }
            }


           homeworkTitle.setChoose(str.toString());
        }
        if(homeworkTypeNumber.getCompletionNumber()>0){
            //布置指定数量的填空题
            List<Integer> completions= homeworkAssignmentsMapper
                    .pageFindCompletion(homeworkTypeNumber);
            StringBuffer str=new StringBuffer();
            for (int i=0;i<completions.size();i++) {
                if(i==completions.size()-1) {
                    str.append(completions.get(i));
                }else  {
                    str.append(completions.get(i)+",");
                }
            }
            homeworkTitle.setCompletion(str.toString());

        }
        if(homeworkTypeNumber.getAnswer_questionsNumber()>0){
            //布置指定数量的解答题
            List<Integer> answer_questions = homeworkAssignmentsMapper
                    .pageFindAnswer_Questions(homeworkTypeNumber);
            StringBuffer str=new StringBuffer();
            for (int i=0;i<answer_questions.size();i++) {
                if(i==answer_questions.size()-1) {
                    str.append(answer_questions.get(i));
                }else  {
                    str.append(answer_questions.get(i)+",");
                }
            }
            homeworkTitle.setAnswer_questions(str.toString());

        }
        if(homeworkTypeNumber.getCompositionNumber()>0){
            //布置指定数量的作文题
            List<Integer> compositions = homeworkAssignmentsMapper
                    .pageFindComposition(homeworkTypeNumber);
            StringBuffer str=new StringBuffer();
            for (int i=0;i<compositions.size();i++) {
                if(i==compositions.size()-1) {
                    str.append(compositions.get(i));
                }else  {
                    str.append(compositions.get(i)+",");
                }
            }
            homeworkTitle.setComposition(str.toString());


        }




        //得到作业信息id
        String id = homeworkAssignmentsMapper.selecthomeworkid(homeworkAssignments.getExpiration_time());
        //添加作业题目
            homeworkTitle.setH_id(id);
        homeworkAssignmentsMapper.insertHomeworkTitle(homeworkTitle);
    }

    //试卷上传
    @Override
    public void insertTest(TestMessage testMessage,String uid) {
       //班级考试
        //查询出教师
      Teacher teacher=homeworkAssignmentsMapper.selectteacher(uid);
        //查询出班级
        List<TeacherClass> teacherClasses = markTestsMapper.selectClassIdBytid(teacher.getId()+"");
        System.out.println(teacherClasses);
        //遍历班级,添加考试
        for(int j=0; j<teacherClasses.size();j++){
            testMessage.setC_id(teacherClasses.get(j).getC_id());
            testMessage.setSubject(teacherClasses.get(j).getSubject());
            testMessage.setLevel(teacher.getLevel());
            testMessage.setStatus("未完成");

            //添加考试信息
            homeworkAssignmentsMapper.insertTest(testMessage);

            //指定题型数量
            HomeworkTypeNumber homeworkTypeNumber=new HomeworkTypeNumber();
            //选择题5道
            homeworkTypeNumber.setChooseNumber(5);
            //填空题5道
            homeworkTypeNumber.setCompletionNumber(5);
            //解答题3道
            homeworkTypeNumber.setAnswer_questionsNumber(3);
            //作文题1道
            homeworkTypeNumber.setCompositionNumber(1);
            //创建存储题目对象
            Titleids titleids=new Titleids();
            //t_id赋值
            titleids.setT_id(homeworkAssignmentsMapper.selectTest(testMessage.getEnd_time(),teacherClasses.get(j).getC_id()));
            //查询题目，给科目赋值
            homeworkTypeNumber.setSubject(testMessage.getSubject());
            if(homeworkTypeNumber.getChooseNumber()>0){
                //布置指定数量的选择题
                List<Integer> chooses= homeworkAssignmentsMapper
                        .pageFindChoose(homeworkTypeNumber);

                StringBuffer str=new StringBuffer();
                for (int i=0;i<chooses.size();i++) {
                    if(i==chooses.size()-1) {
                        str.append(chooses.get(i));
                    }else  {
                        str.append(chooses.get(i)+",");
                    }
                }

                titleids.setChoose(str.toString());
            }
            if(homeworkTypeNumber.getCompletionNumber()>0){
                //布置指定数量的填空题
                List<Integer> completions= homeworkAssignmentsMapper
                        .pageFindCompletion(homeworkTypeNumber);
                StringBuffer str=new StringBuffer();
                for (int i=0;i<completions.size();i++) {
                    if(i==completions.size()-1) {
                        str.append(completions.get(i));
                    }else  {
                        str.append(completions.get(i)+",");
                    }
                }
                titleids.setCompletion(str.toString());

            }
            if(homeworkTypeNumber.getAnswer_questionsNumber()>0){
                //布置指定数量的解答题
                List<Integer> answer_questions = homeworkAssignmentsMapper
                        .pageFindAnswer_Questions(homeworkTypeNumber);
                StringBuffer str=new StringBuffer();
                for (int i=0;i<answer_questions.size();i++) {
                    if(i==answer_questions.size()-1) {
                        str.append(answer_questions.get(i));
                    }else  {
                        str.append(answer_questions.get(i)+",");
                    }
                }
                titleids.setAnswer_questions(str.toString());

            }
            if(homeworkTypeNumber.getCompositionNumber()>0){
                //布置指定数量的作文题
                List<Integer> compositions = homeworkAssignmentsMapper
                        .pageFindComposition(homeworkTypeNumber);
                StringBuffer str=new StringBuffer();
                for (int i=0;i<compositions.size();i++) {
                    if(i==compositions.size()-1) {
                        str.append(compositions.get(i));
                    }else  {
                        str.append(compositions.get(i)+",");
                    }
                }
                titleids.setComposition(str.toString());


            }

            //添加考试题目
            homeworkAssignmentsMapper.insertTestTitle(titleids);
        }

    }
}
